#include "ControllablePredator.h"
#include "EventPoller.h"
#include "Timer.h"

ControllablePredator::ControllablePredator(Vec3f pos, int id)
{
	m_pos = pos;
	m_id = id;
	m_moveSpeed = 3;

	m_movingUp = false;
	m_movingDown = false;
	m_movingLeft = false;
	m_movingRight = false;


	TheEventPoller::Instance()->AddHandler(this);
}

ControllablePredator::~ControllablePredator()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void ControllablePredator::Update()
{
	float dt = TheTimer::Instance()->GetDT();

	if(m_movingUp && m_movingDown)
	{
		m_vel.Z = 0;
	}
	else if(m_movingUp)
	{
		m_vel.Z = -m_moveSpeed;
	}
	else if(m_movingDown)
	{
		m_vel.Z = m_moveSpeed;
	}
	else 
	{
		m_vel.Z = 0;
	}

	if(m_movingLeft && m_movingRight)
	{
		m_vel.X = 0;
	}
	else if(m_movingLeft)
	{
		m_vel.X = -m_moveSpeed;
	}
	else if(m_movingRight)
	{
		m_vel.X = m_moveSpeed;
	}
	else
	{
		m_vel.X = 0;
	}

	m_pos += m_vel * dt;
}

void ControllablePredator::OnKeyboardEvent(const SDL_KeyboardEvent& k)
{
	if((k.keysym.sym == SDLK_UP || k.keysym.sym == SDLK_w)  && k.state == SDL_PRESSED)
	{
		m_movingUp = true;
	}
	else if((k.keysym.sym == SDLK_UP || k.keysym.sym == SDLK_w) && k.state == SDL_RELEASED) 
	{
		m_movingUp = false;
	}
	if((k.keysym.sym == SDLK_DOWN || k.keysym.sym == SDLK_s)  && k.state == SDL_PRESSED)
	{
		m_movingDown = true;
	}
	else if((k.keysym.sym == SDLK_DOWN || k.keysym.sym == SDLK_s) && k.state == SDL_RELEASED) 
	{
		m_movingDown = false;
	}
	if((k.keysym.sym == SDLK_LEFT || k.keysym.sym == SDLK_a)  && k.state == SDL_PRESSED)
	{
		m_movingLeft = true;
	}
	else if((k.keysym.sym == SDLK_LEFT || k.keysym.sym == SDLK_a) && k.state == SDL_RELEASED) 
	{
		m_movingLeft = false;
	}
	if((k.keysym.sym == SDLK_RIGHT || k.keysym.sym == SDLK_d)  && k.state == SDL_PRESSED)
	{
		m_movingRight = true;
	}
	else if((k.keysym.sym == SDLK_RIGHT || k.keysym.sym == SDLK_d) && k.state == SDL_RELEASED) 
	{
		m_movingRight = false;
	}
}
